﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using Cinemachine;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace Adventure
{
    public class Map : Entity
    {
        [HideInInspector]
        public Vector3 playerPoint;

        private MapData mapData;

        public enum EnemyType {野猪,蜗牛, 蜜蜂, };
        private Transform EnemyType_01, EnemyType_02, EnemyType_03;
        private List<Vector3> EnemyVect_01, EnemyVect_02, EnemyVect_03;
        private Dictionary<EnemyType, List<Vector3>> EnemyInfo;

        public List<int> EnemyTipeId_01, EnemyTipeId_02, EnemyTipeId_03;

        protected override void OnInit(object userData)
        {
            base.OnInit(userData);

            if (mapData != null)
            {
                mapData = (MapData)userData;
            }

            EnemyType_01 = transform.FindTransform<Transform>("EnemyType_01");
             EnemyType_02 = transform.FindTransform<Transform>("EnemyType_02");
             EnemyType_03 = transform.FindTransform<Transform>("EnemyType_03");

            EnemyInfo = new Dictionary<EnemyType, List<Vector3>>();

            EnemyVect_01 = new List<Vector3>();
            EnemyVect_02 = new List<Vector3>();
            EnemyVect_03 = new List<Vector3>();

            EnemyTipeId_01 = new List<int>();
            EnemyTipeId_02 = new List<int>();
            EnemyTipeId_03 = new List<int>();

            for (int i = 0; i < EnemyType_01.childCount; i++)
            {
                EnemyVect_01.Add(EnemyType_01.GetChild(i).transform.position);
                EnemyTipeId_01.Add(int.Parse(EnemyType_01.GetChild(i).name));
            }
            for (int i = 0; i <  EnemyType_02.childCount; i++)
            {
                EnemyVect_02.Add( EnemyType_02.GetChild(i).transform.position);
                EnemyTipeId_02.Add(int.Parse(EnemyType_02.GetChild(i).name));
            }
            for (int i = 0; i <  EnemyType_03.childCount; i++)
            {
                EnemyVect_03.Add( EnemyType_03.GetChild(i).transform.position);
                EnemyTipeId_03.Add(int.Parse(EnemyType_03.GetChild(i).name));
            }

            EnemyInfo.Add(EnemyType.野猪,EnemyVect_01);
            EnemyInfo.Add(EnemyType.蜗牛,EnemyVect_02);
            EnemyInfo.Add(EnemyType.蜜蜂,EnemyVect_03);
        }
        protected override void OnShow(object userData)
        {
            base.OnShow(userData);
            SetCameraArea();
            //子物体的层级会递归设置和对象身上一样，所以地图包含其他，比如说摄像机范围，不能设置
            transform.Find("Grid").gameObject.SetLayerRecursively(Constant.Layer.GroundId);

           
            playerPoint = transform.Find("PlayerPoint").position;


        }


        public void SetCameraArea()
        {
            Camera.main.GetComponent<CinemachineConfiner2D>().InvalidateCache();
            Camera.main.GetComponent<CinemachineConfiner2D>().m_BoundingShape2D = transform.FindTransform<PolygonCollider2D>("CameraArea");
        }

        public Dictionary<EnemyType, List<Vector3>> GetEnemyInfo() { return EnemyInfo; }
    }
}
